package entity

import (
	"bytes"
	"strings"

	"shqsoft.cn/pasnj/base"
)

//ParentMenu Ods父级菜单
type ParentMenuOds struct {
	ID   int    `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	Name string `gorm:"column:PermissionName;size:20"`
}

func (ParentMenuOds) TableName() string {
	return "V_PermissionOds"
}

//MenuOds Ods系统菜单
type MenuOds struct {
	ID               int           `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	No               string        `gorm:"column:PermissionId;size:50"`
	ParentID         int           `gorm:"column:ParentPermissionId"`
	Name             string        `gorm:"column:PermissionName;size:50"`
	PermissionType   string        `gorm:"column:PermissionType;size:50"`
	Parent           ParentMenuOds `gorm:"ForeignKey:ParentPermissionId`
	Url              string        `gorm:"column:Url;size:50"`
	IsUse            bool          `gorm:"column:ISQY"`
	SortNo           int           `gorm:"column:SortNo"`
	MenusCaptions    string        `gorm:"-"`
	ReactComponent   string        `gorm:"column:ReactComponent"`
	ReactPorps       string        `gorm:"column:ReactPorps"`
	ReactIcon        string        `gorm:"column:ReactIcon"`
	UserPopedomTypes []string      `gorm:"-"`
	SJY              string        `gorm:"column:SJY"`
}

func (m *MenuOds) AfterFind() (err error) {
	if m.PermissionType != "" {
		ids := strings.Split(m.PermissionType, ",")
		sql := "select PermissionTypeDes from t_permissiontype where PermissionType in (?)"
		rows, err := base.DB.Raw(sql, ids).Rows()
		if err != nil {
			return err
		}
		var buff bytes.Buffer
		defer rows.Close()
		for rows.Next() {
			var caption string
			rows.Scan(&caption)
			buff.WriteString(caption)
			buff.WriteString(",")
		}
		if l := buff.Len(); l > 0 {
			buff.Truncate(l - 1)
		}
		m.MenusCaptions = buff.String()
	}
	return
}

//TableName MenuOds 系统菜单表名
func (MenuOds) TableName() string {
	return "t_PermissionOds"
}
func MenuOdsEF() base.EntityFunc {
	return func() interface{} {
		entity := MenuOds{}
		return &entity
	}
}
func MenuOdsESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []MenuOds
		return &entities
	}
}

//ParentMenu
func ParentMenuOdsEF() base.EntityFunc {
	return func() interface{} {
		entity := ParentMenuOds{}
		return &entity
	}
}
func ParentMenuOdsESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []ParentMenuOds
		return &entities
	}
}
func init() {
	base.DaoPoolObj.Put("MenusType", base.NewDefaultDao(MenusTypeEF(), MenusTypeESF(), nil))
	base.DaoPoolObj.Put("ParentMenuOds", base.NewDefaultDao(ParentMenuOdsEF(), ParentMenuOdsESF(), nil))
	relationship := []string{"Parent"}
	base.DaoPoolObj.Put("MenuOds", base.NewDefaultDao(MenuOdsEF(), MenuOdsESF(), relationship))
}
